﻿using System;
using System.Configuration;
using Interfaces.Validation.Services;

namespace BusinessLogic.Services.Validation
{
    public class SessionValidationService : ISessionValidationService
    {
        private const string SessionExpiredKey = "sessionExpiredHours";
        private const int DefaultSessionExpiredHours = 24;

        public bool IsSessionExpired(DateTime createdDate)
        {
            return CalculateExpiredTime(createdDate) < DateTime.Now;
        }

        private static int GetExpiredHours()
        {
            var stringHours = ConfigurationManager.AppSettings[SessionExpiredKey];
            int hours;
            int.TryParse(stringHours, out hours);
            return hours != 0 ? hours : DefaultSessionExpiredHours;
        }

        private static DateTime CalculateExpiredTime(DateTime lastUpdatedTime)
        {
            return lastUpdatedTime.AddHours(GetExpiredHours());
        }
    }
}
